﻿Public Class pagocredito 
    Dim x As Integer = 0
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
    Private Sub pagocredito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.nominastipo' table. You can move, or remove it, as needed.
        Me.NominastipoTableAdapter.Fill(Me.DbFireAndIceDataSet.nominastipo)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.creditos' table. You can move, or remove it, as needed.
        Me.CreditosTableAdapter.Fill(Me.DbFireAndIceDataSet.creditos)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.clientes' table. You can move, or remove it, as needed.
        Me.ClientesTableAdapter.Fill(Me.DbFireAndIceDataSet.clientes)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.pagocredito' table. You can move, or remove it, as needed.
        Me.PagocreditoTableAdapter.Fill(Me.DbFireAndIceDataSet.pagocredito)

    End Sub

    Private Sub PagocreditoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PagocreditoBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PagocreditoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

   

    Private Sub MontopagoTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
            MsgBox("Solo puede ingresar números", MsgBoxStyle.Information, "Pago de creditos")
            MontopagoTextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            MsgBox("No debe ingresar espacios", MsgBoxStyle.Information, "Pagos de creditos")
            MontopagoTextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub IdclienteLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdclienteLookUpEdit.EditValueChanged
        If Not IsDBNull(IdclienteLookUpEdit.EditValue) Then

            txtnombre.EditValue = IdclienteLookUpEdit.GetColumnValue("nombre")
            txtapellido.EditValue = IdclienteLookUpEdit.GetColumnValue("apellido")
            txttelefono.EditValue = IdclienteLookUpEdit.GetColumnValue("telefono")
            PictureEdit1.EditValue = IdclienteLookUpEdit.GetColumnValue("fotografia")


        End If
    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Me.PagocreditoTableAdapter.Fill(Me.DbFireAndIceDataSet.pagocredito)
        Mostrar()
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
        ErrorProvider1.SetError(MontopagoTextBox, Nothing)
        ErrorProvider1.SetError(IdtipopagoLookUpEdit, Nothing)
        ErrorProvider1.SetError(IdcreditoLookUpEdit, Nothing)
        Mostrar()
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
    End Sub


  

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()

    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion2 As Integer
        opcion2 = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion2 = vbYes Then
            Try
                PagocreditoTableAdapter.DeleteQuery(IdpagocreditoTextBox.Text)
                Me.PagocreditoTableAdapter.Fill(Me.DbFireAndIceDataSet.pagocredito)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click


        If txtnombre.Text = "" Then
            ErrorProvider1.SetError(IdclienteLookUpEdit, "Seleccione un cliente")
            Return
            ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
        End If


        If txtvalorapagar.Text = "" Then
            ErrorProvider1.SetError(IdcreditoLookUpEdit, "Seleccione una Hoja de credito")
        Else
            ErrorProvider1.SetError(txtvalorapagar, Nothing)
        End If


        If txtnomina.Text = "" Then
            ErrorProvider1.SetError(IdtipopagoLookUpEdit, "Seleccione un tipo de pago")
        Else
            ErrorProvider1.SetError(IdtipopagoLookUpEdit, Nothing)
        End If


        If MontopagoTextBox.Text = "L 00.00" Then

            ErrorProvider1.SetError(MontopagoTextBox, "Ingrese el pago a efectuar")
        Else
            ErrorProvider1.SetError(MontopagoTextBox, Nothing)
        End If

        If (txtnombre.Text <> "") And (MontopagoTextBox.Text <> "L 00.00") And (txtnomina.Text <> "") And (txtfechacredito.Text <> "") Then
            ErrorProvider1.SetError(MontopagoTextBox, Nothing)
            ErrorProvider1.SetError(IdtipopagoLookUpEdit, Nothing)
            ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
            ErrorProvider1.SetError(IdcreditoLookUpEdit, Nothing)
            PagocreditoBindingNavigatorSaveItem.PerformClick()
            MessageBox.Show("Se guardó el resgistro satisfactoriamente")
            PagocreditoTableAdapter.InsertQuerycreditos(IdpagocreditoTextBox.Text, IdclienteLookUpEdit.EditValue, IdcreditoLookUpEdit.EditValue, IdtipopagoLookUpEdit.EditValue, Date.Now, MontopagoTextBox.Text)
            Mostrar()
        End If
    End Sub

    Private Sub MontopagoTextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MontopagoTextBox.Leave

        If txtvalorapagar.Text > MontopagoTextBox.Text Then
            MsgBox("El pago no debe ser menor al crédito otorgado", MsgBoxStyle.Information, "Pago de creditos")
        End If



    End Sub

    Private Sub IdtipopagoLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdtipopagoLookUpEdit.EditValueChanged
        txtnomina.EditValue = IdtipopagoLookUpEdit.GetColumnValue("descripcion")
    End Sub

    Private Sub IdcreditoLookUpEdit_EditValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdcreditoLookUpEdit.EditValueChanged
        If Not IsDBNull(IdcreditoLookUpEdit.EditValue) Then
            txtfechacredito.EditValue = IdcreditoLookUpEdit.GetColumnValue("fechacredito")
            txtvalorapagar.EditValue = IdcreditoLookUpEdit.GetColumnValue("montocredito")

        End If


    End Sub
End Class